<template>
    <div>
        <el-dialog
            :title="$t('add')"
            :visible="visibleAdd"
            class="el-dialog-sm"
            @close="()=>{$store.commit('setVisibleAdd',false)}">
            <el-form ref="form" :model="form" label-width="200px" >
                <el-row :gutter="24">
                    <el-col :span="20">
                        <el-form-item :label="$t('device-type')" prop="type" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.type" placeholder="" @change="changeType">
                                <el-option
                                    v-for="item in deviceTypeList"
                                    :key="item.value"
                                    :label="item.code"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('devWifi.deviceType')" prop="inverterType" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.inverterType" placeholder="">
                                <el-option
                                    v-for="item in typeData"
                                    :key="item.value"
                                    :label="item.code"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('device-sn-title')" prop="prefix" :rules="[{ required: form.type=='0'?true:false, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.prefix" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('device-installation-guide')" prop="file" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <baseFile v-model="form.file" ></baseFile>
                        </el-form-item>
                        <el-form-item :label="$t('installationGuide.title')" prop="guideFile" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <baseFile v-model="form.guideFile" ></baseFile>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleAdd',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('save')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseFile from '../../../components/common/baseFileInput'
export default {
    components:{
        baseFile
    },
    props: {
        deviceTypeList:'',
        typeDataObj:''
    },
    data() {
        return {
            typeData:[],
            form:{
                type:'',
                inverterType:'',
                prefix:'',
                file:'',
                guideFile:'',
            }
        };
    },
    computed: {
        visibleAdd(){
            return this.$store.state.sysStore.visibleAdd
        }
    },
    mounted(){
      
    },
    methods: {
        changeType(val){
            this.typeData=this.typeDataObj[val]
            this.form.inverterType=''
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                this.commitForm()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
        async commitForm(){
            let res = await this.API.addGuide(this.form)
            if(res.success){
                this.$message.success(this.$t('index.control_success'));
            }else{
                this.$message.error(this.$t('index.control_fail'));
            }
            this.$store.commit('setVisibleAdd',false)
            this.$emit('uploadPage',true)
        }
    }
};
</script>

<style  lang="less">
</style>
